package ch.pizzapp.kurier;

import java.util.List;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.FragmentArg;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.rest.RestService;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import ch.pizzapp.kurier.adapter.GridNormalPizzaWithoutSizeAdapater;
import ch.pizzapp.kurier.client.InfoClient;
import ch.pizzapp.kurier.data.CategoryData;
import ch.pizzapp.kurier.data.GenarateItem;
import ch.pizzapp.kurier.data.PizaaWithoutSizeResponceData;
import ch.pizzapp.kurier.data.PlaceData;
import ch.pizzapp.kurier.dto.InfoDto;

/**
 * A simple {@link android.support.v4.app.Fragment} subclass. Use the
 * {@link PizzaWithoutSizeFragment#newInstance} factory method to create an
 * instance of this fragment.
 * 
 */
@EFragment(R.layout.fragment_pizza_with_out_size)
public class PizzaWithoutSizeFragment extends BaseFragmnet {

	// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
	private static final String CATEGORY_DATA = "category";

	private static final String PLACE_DATA = "PlaceData";

	// @ViewById(R.id.gridView)
	// GridView gridView;

	private Callback mCallback;

	public void setOnCallback(Callback callback) {
		this.mCallback = callback;
	}

	public interface Callback {
		void addToCart(GenarateItem genarateItem);

	}

	@Bean
	InfoDto infoDto;

	@RestService
	InfoClient infoClient;

	// private String mParam1;
	// private String mParam2;
	@FragmentArg(CATEGORY_DATA)
	CategoryData categoryData;

	@FragmentArg(PLACE_DATA)
	PlaceData placeData;

	@Bean
	GridNormalPizzaWithoutSizeAdapater gridNormalAdapater;

	private List<GenarateItem> pizaaWithoutSizeResponce;

	// @ViewById(R.id.text)
	// TextView text;

	public static PizzaWithoutSizeFragment newInstance(
			CategoryData categoryData, PlaceData placeDataItem) {
		PizzaWithoutSizeFragment fragment = new PizzaWithoutSizeFragment_();
		Bundle args = new Bundle();
		args.putParcelable(CATEGORY_DATA, categoryData);
		args.putParcelable(PLACE_DATA, placeDataItem);
		fragment.setArguments(args);
		return fragment;
	}

	@AfterViews
	void afterView() {
		// text.setText("name" + categoryData.getTitle());

		if (placeData != null) {

			String titleString = placeData.getTitle() + ": Delivery Time: ca"
					+ placeData.getOrdertime() + "\n Minimum Oder Value: "
					+ placeData.getMinorder()
					+ getActivity().getResources().getString(R.string.crurncey);

			title.setText(titleString);
		}

		getItems();

		gridNormalAdapater
				.setOnCallback(new GridNormalPizzaWithoutSizeAdapater.Callback() {

					@Override
					public void addToCart(GenarateItem genarateItem) {

						if (mCallback != null) {
							mCallback.addToCart(genarateItem);
						}

					}
				});

	}

	@Background
	void getItems() {

		// System.out.println("categoryData.getCategorytype() "
		// + categoryData.getCategorytype() + " categoryData.getCatID()"
		// + categoryData.getCatID());

		infoDto.setCategorytype(categoryData.getCategorytype());
		infoDto.setCatID(categoryData.getCatID());
		infoDto.setType("8");

		new ReachabilityTest(getActivity(), new ReachabilityTest.Callback() {

			@Override
			public void onReachabilityTestPassed() {

				try {
					setGenarate(infoClient.getPizzawithOutSize(infoDto
							.getMultiValuedMap()));
				} catch (Exception e) {

					System.err.println(e);

				}
			}

			@Override
			public void onReachabilityTestFailed() {
				showProgress(false);
				showMessge("Not connected ");
			}
		});
	}

	// @Trace
	@UiThread
	void setGenarate(PizaaWithoutSizeResponceData pizaaWithoutSizeResponceData) {

		pizaaWithoutSizeResponce = pizaaWithoutSizeResponceData.getResponse()
				.getItems();

		showProgress(false);

		gridNormalAdapater.setGenarateItemList(pizaaWithoutSizeResponce);

		gridView.setAdapter(gridNormalAdapater);

		gridView.setAnimationCacheEnabled(false);
		gridView.setScrollingCacheEnabled(false);

	}

	public void minus(int index) {

		gridView.invalidate();

		int visiblePosition = gridView.getFirstVisiblePosition();
		View view = gridView.getChildAt(index - visiblePosition);
		gridView.getAdapter().getView(index, view, gridView);
	}

	@UiThread
	void showMessge(final String msg) {
		Toast.makeText(getActivity(), msg, Toast.LENGTH_LONG).show();
	}

	public PizzaWithoutSizeFragment() {
		// Required empty public constructor
	}

}
